package com.yanqu.road.server.channelserver.manager.activity.tonbing.activity;

import com.yanqu.road.entity.activity.ActivityConditionInfo;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.activity.tonbingchannel.config.ChannelTonBingConfig;
import com.yanqu.road.entity.enums.activity.tonbing.eTonBingTaskType;
import com.yanqu.road.entity.enums.eGamePlayerEventType;
import com.yanqu.road.server.channelserver.manager.activity.ChannelActivityInfoMgr;
import com.yanqu.road.server.channelserver.manager.activity.tonbing.task.BaseChannelTonBingMatchTask;
import com.yanqu.road.server.manager.scene.CrossSceneMgr;
import com.yanqu.road.utils.string.StringUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import java.util.List;
import java.util.concurrent.DelayQueue;

/**
 * 放一些配置的东西
 */
public class ChannelTonBingBaseMatch extends ChannelTonBingBase {

    protected static Logger logger = LogManager.getLogger(ChannelTonBingBaseMatch.class.getName());

    // ============================== 锁 ==============================

    /**
     * 大锁
     */
    protected final Object lock = new Object();

    // ============================== 停线程 ==============================

    private boolean isStop = false;

    private boolean isGameOver = false;

    public void setStop(boolean stop) {
        isStop = stop;
    }

    public void setGameOver(boolean isOver){
        isGameOver = isOver;
    }

    public boolean isGameOver() {
        return isGameOver;
    }

    // ============================== 定时器 ==============================

    /**
     * 单独线程
     */
    protected Thread thread = new Thread(new DelayQueueRun());

    protected class DelayQueueRun implements Runnable {
        @Override
        public void run() {
            while (true) {

                if (isStop) {
                    return;
                }

                try {
                    BaseChannelTonBingMatchTask task = taskQueue.take();

                    // 活动结束的任务：
                    if (task.getBeginTime() > config.getActivityInfo().getEndTime() * 1000L) {
                        if (task.getType() != eTonBingTaskType.SetWinner
                                && task.getType() != eTonBingTaskType.SendUnionHonor) {
                            continue;
                        }
                    }

                    // 正常执行
                    try {
                        task.run();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    /**
     * 定时器执行队列
     */
    protected DelayQueue<BaseChannelTonBingMatchTask> taskQueue = new DelayQueue<>();

    /**
     * 添加task，随便加，使用时会再校验时间
     */
    public void addTask(BaseChannelTonBingMatchTask task) {
        taskQueue.add(task);
    }

    // ============================== 配置 ==============================

    /**
     * 活动配置
     */
    protected ChannelTonBingConfig config;

    /**
     * 榜单长度
     */
    protected int userMaxRankConfig;
    protected int unionMaxRankConfig;
    protected int campMaxRankConfig;

    protected ChannelTonBingBaseMatch(ChannelTonBingConfig tempConfig) {
        // 读配置库
        loadConfigFromDb(tempConfig.getActivityInfo());
        // 加场景
        CrossSceneMgr.initActivity(tempConfig.getActivityInfo());
        config = tempConfig;

        serverIdlIst = config.getActivityInfo().getServerIdList();
        channelIdList = StringUtils.stringToIntegerList(config.getActivityInfo().getParam6(),";");
    }

    public ChannelTonBingConfig getConfig() {
        return config;
    }

    private void initMaxRankConfig(int activityId) {
        userMaxRankConfig = getMaxRankConfig(ChannelActivityInfoMgr.getConditionInfoList(activityId, eGamePlayerEventType.TonBingUserZhanGongRank.getValue()));
        unionMaxRankConfig = getMaxRankConfig(ChannelActivityInfoMgr.getConditionInfoList(activityId, eGamePlayerEventType.TonBingCampUnionZhanGongRank.getValue()));
        campMaxRankConfig = getMaxRankConfig(ChannelActivityInfoMgr.getConditionInfoList(activityId, eGamePlayerEventType.TonBingCampScoreRank.getValue()));
    }

    public void loadConfigFromDb(ActivityInfo activityInfo) {
        initMaxRankConfig(activityInfo.getActivityId());
    }

    private int getMaxRankConfig(List<ActivityConditionInfo> list) {
        int maxRank = 0;
        for (ActivityConditionInfo conditionInfo : list) {
            int tmp = conditionInfo.getParamList().get(1).intValue();
            if (tmp > 0 && maxRank < tmp) {
                maxRank = tmp;
            }
        }
        return maxRank;
    }
}
